import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.util.*;
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import ca.odell.glazedlists.*;
import ca.odell.glazedlists.swing.*;

class AddGUI extends JPanel implements ActionListener, Runnable{

private JButton addButton;
private JPanel addPanel;
private JPanel buttonPanel;
private JFrame addFrame;
private EventList evt;
private JFileChooser chooser;
private DBFile dbFile;
private String str;
private ConfigurationSingleton config;
private LinkedList selectedFiles;
private File[] files;
private File file;
private JTextField userInputText;
private JLabel userInput;
private JPanel userInputPanel;

public AddGUI(String str, EventList evt){
  this.evt = evt;
  this.str = str;
}

public AddGUI(String str, LinkedList selectedFiles){
  this.str = str;
  this.selectedFiles = selectedFiles;
}

public AddGUI(String str, ConfigurationSingleton config){
  this.str = str;
  this.config = config;
}


public void run(){
   FileBrowserGUI.setLockSyncThreadTrue();
   JPanel addPanel = new JPanel();
   userInputPanel = new JPanel();
   addPanel.setLayout(new BorderLayout());
      if(str.equals("Set Launch Application For File")){
         chooser = new JFileChooser("C:Program Files");
         chooser.setMultiSelectionEnabled(false);
         ExampleFileFilter exeFilter = new ExampleFileFilter("exe", "EXE Executable Files");
         chooser.addChoosableFileFilter(exeFilter);
      }
      else if(str.equals("Set Scan Directories")){
         chooser = new JFileChooser();
         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
         chooser.setMultiSelectionEnabled(true);
      }
      else if(str.equals("Add File")){
         chooser = new JFileChooser();
         chooser.setMultiSelectionEnabled(true);
      }
      else if(str.equals("Set Default Launch Application For File Type")){
         chooser = new JFileChooser("C:Program Files");
         chooser.setMultiSelectionEnabled(false);
         ExampleFileFilter exeFilter = new ExampleFileFilter("exe", "EXE Executable Files");
         chooser.addChoosableFileFilter(exeFilter);
         userInput = new JLabel("File Extension:");
         userInputText = new javax.swing.JTextField(10);
         userInputPanel = new JPanel();
         userInputPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
         userInputPanel.add(userInput);
         userInputPanel.add(userInputText);
      }

   chooser.setControlButtonsAreShown(false);

   addPanel.add(chooser, BorderLayout.NORTH);

   if(str.equals("Set Default Launch Application For File Type")){
     addPanel.add(userInputPanel, BorderLayout.SOUTH);
   }
   buttonPanel = new JPanel();
   buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

   addButton = new JButton("Add");
   addButton.addActionListener(this);
   buttonPanel.add(addButton);

   addFrame = new JFrame(str);

   addFrame.setLayout(new BorderLayout());
   addFrame.getContentPane().add(addPanel, BorderLayout.NORTH);
   addFrame.add(buttonPanel, BorderLayout.SOUTH);

   addFrame.setSize(600,500);
   addFrame.show();
}

private void getFiles(){
   if(str.equals("Add File") || str.equals("Set Scan Directories")){
      files = chooser.getSelectedFiles();
   }
   else{
      file = chooser.getSelectedFile();
   }
}

private void addFiles(){
   File fi;
   for(int i=0;i<files.length;i++){
      fi = (File)files[i];
      String abs_path = fi.getPath();
      dbFile = new DBFile(abs_path);
      dbFile.setMarkProtected();
        if(evt.contains(dbFile)==false){
           evt.add(dbFile);
           addFrame.hide();
        }
        else {
           addFrame.hide();
           JOptionPane.showMessageDialog(new JFrame(),
           abs_path+" already exists",
           "Error",
           JOptionPane.ERROR_MESSAGE);
        }
    }
 }

private void setFileLaunchApp(){
   String abs_path = file.getPath();
   Iterator fileIter = selectedFiles.listIterator();
      while(fileIter.hasNext()){
         DBFile temp = (DBFile)fileIter.next();
         temp.setLaunchApp(abs_path);
         System.out.println(temp.getName()+" "+abs_path);
      }
 }

 private void setExtLaunchApp(){
    String ext = userInputText.getText();
    if(file != null && !ext.equals("")){
       String input = userInputText.getText().toLowerCase();
       System.out.println(input);
       config.setLaunchApp(input,file.getPath());
    }
    else{
       addFrame.hide();
              JOptionPane.showMessageDialog(new JFrame(),
              "You must select a launch application and enter an extension.",
                         "Error",
                         JOptionPane.ERROR_MESSAGE);
    }
 }

private void setScanDirectories(){
   File fi;
   config.clearScanDirectories();
     for(int i=0;i<files.length;i++){
        fi = (File)files[i];
        config.addScanDirectory(fi);
     }
}

public void actionPerformed(ActionEvent e) {
   Object source = e.getSource();
      if(source == addButton){
         getFiles();
         if(str.equals("Add File")){
            addFiles();
         }
         else if(str.equals("Set Launch Application For File")){
            setFileLaunchApp();
         }
         else if(str.equals("Set Scan Directories")){
            setScanDirectories();
         }
         else if(str.equals("Set Default Launch Application For File Type")){
            setExtLaunchApp();
         }
         FileBrowserGUI.setLockSyncThreadFalse();
         addFrame.hide();

      }
 }
}
